package com.personal.kousuan;

import com.personal.kousuan.db.CommonData;
import com.personal.kousuan.db.InitialData;
import com.personal.kousuan.db.MyDbHelper;
import com.personal.kousuan.db.MyDbInfo;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;

public class MainFragmentActivity extends FragmentActivity implements
		OnClickListener {

	public static final int TAB_HOME = 0;
	public static final int TAB_CATAGORY = 1;

	private ViewPager viewPager;
	private RadioButton main_tab_home, main_tab_catagory;
	public static MyDbHelper db = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_main);
		initialDbData();
		CommonData.getInstance().load(this);
		initView();
		addListener();

	}
	
	private void initialDbData(){
		db = MyDbHelper.getInstance(getApplicationContext());
		db.open();
		
		Cursor cursor = db.select(MyDbInfo.getTableNames()[0], MyDbInfo.getFieldNames()[0], null, null, null, null, null);
		if(cursor.moveToNext()){
			cursor.close();
			return;
		}
		
		String[] semester = InitialData.INITIAL_SEMESTER;
		int index = 0;
		for(int i = 0;i<semester.length;i++){
			db.insert(MyDbInfo.getTableNames()[0],MyDbInfo.getFieldNames()[0],new String[]{String.valueOf(i),semester[i]});
			
			String[][] category = InitialData.INITIAL_CATEGORY[i];
			for(int j = 0;j<category.length;j++){
				db.insert(MyDbInfo.getTableNames()[1], MyDbInfo.getFieldNames()[1], new String[]{String.valueOf(index),String.valueOf(i),category[j][0],category[j][1]});
				index++;
				}
			}
		}

	
	private void initView() {

		viewPager = (ViewPager) findViewById(R.id.viewpager);
		main_tab_home = (RadioButton) findViewById(R.id.main_tab_home);
		main_tab_catagory = (RadioButton) findViewById(R.id.main_tab_catagory);

		main_tab_home.setOnClickListener(this);
		main_tab_catagory.setOnClickListener(this);

		
		FragmentAdapter adapter = new FragmentAdapter(
				getSupportFragmentManager());
		viewPager.setAdapter(adapter);

	}
	
	private void addListener() {
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int id) {
				switch (id) {
				case TAB_HOME:
					main_tab_home.setChecked(true);
					break;
				case TAB_CATAGORY:
					main_tab_catagory.setChecked(true);
					break;

				default:
					break;
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
	}

	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.main_tab_home:
			viewPager.setCurrentItem(TAB_HOME);
			break;
		case R.id.main_tab_catagory:
			viewPager.setCurrentItem(TAB_CATAGORY);
			break;
		default:
			break;
		}		
	}




}
